package com.pantuo.sys.controller;

import com.pantuo.common.utils.TableRequest;
import com.pantuo.common.vo.JsonResult;
import com.pantuo.sys.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/dbApi")
public class CommonController {

    @Autowired
    private CommonService commonService;

    /**
     * 通用保存与修改
     *
     * @param params
     * @return
     */
    @RequestMapping("/saveModel")
    public JsonResult doSaveOrUpdateObject(@RequestParam Map<String, Object> params) {
        if (params.get("id") != null) {
            commonService.saveOrUpdate(params);
            return new JsonResult("update ok");
        } else {
            commonService.saveOrUpdate(params);
            return new JsonResult("save ok");
        }
    }


    /**
     * 通用删除：根据id删除
     *
     * @param className
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    public JsonResult doDeleteObject(String className, Integer... id) {
        commonService.delById(className, id);
        return new JsonResult("delete ok");
    }

    /**
     * 通用单表分页并按条件查询（可以任何条件查询）
     * @param request
     * @param className
     * @return
     */
    /*@RequestMapping("/doFindPageObjects")
    public JsonResult doFindPageObjects(TableRequest request, @RequestParam(required = true) String className) throws IOException {
        String tableName = className.split("_")[1];
        String dbName = className.split("_")[0];
        if (dbName.contains("b")){
            System.out.println("----------------");
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUrl("jdbc:mysql://10.115.3.205:3307/oa_data4?serverTimezone=GMT%2B8&characterEncoding=utf8");
            druidDataSource.setUsername("root");
            druidDataSource.setPassword("pantuo1709");
            DynamicDataSource.setDataSource(druidDataSource);
        }
        if (dbName.contains("c")){
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUrl("jdbc:mysql://10.115.3.205:3307/oa_data5?serverTimezone=GMT%2B8&characterEncoding=utf8");
            druidDataSource.setUsername("root");
            druidDataSource.setPassword("pantuo1709");
            DynamicDataSource.setDataSource(druidDataSource);
            DynamicDataSource.clear();
        }

        PageImpl<Map<String, Object>> pageObjects = commonService.findPageObjects(request, tableName);
        DynamicDataSource.clear();
        return new JsonResult(pageObjects);

    }*/

    /**
     * 通用分页与查询(查询条件任意)
     *
     * @param request
     * @param className
     * @return
     * @throws IOException
     */
    @RequestMapping("/getModelList")
    public JsonResult doFindPageObjects(String className, TableRequest request) throws IOException {
        System.out.println("adfadfad");
        System.out.println(request);
        System.out.println("className：" + className);
        PageImpl<Object> modelList = commonService.getModelList(request, className);
        return new JsonResult(commonService.getModelList(request, className));
    }

    /**
     * 通用根据数据库字段删除数据
     *
     * @param dbField
     * @param value
     * @param className
     * @return
     */
    @RequestMapping("/delByField")
    public JsonResult doDeleteByField(String dbField, String value, String className) {
        commonService.delByField(dbField, value, className);
        return new JsonResult("delete ok");
    }

    /**
     * 通用根据id查询
     *
     * @param id
     * @param className
     * @return
     */
    @RequestMapping("/queryById")
    public JsonResult doQueryById(String id, String className) {
        Map<String, Object> obj = commonService.queryById(id, className);
        System.out.println(obj);
        return new JsonResult(obj);
    }

    @RequestMapping("/t1")
    public String test1(){
        return "dbapi";
    }

    @RequestMapping("/getModelList2")
    public JsonResult do1(String className, TableRequest request) throws IOException {
        System.out.println("adfadfad");
        System.out.println(request);
        System.out.println("className：" + className);
        return new JsonResult(commonService.getModelList(request, className));
    }
    
}
